/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slsBeans;

import EL.Accounts;
import EL.ReservationDetails;
import EL.ReservationDetailsPK;
import EL.Reservations;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class ReservationBLO implements ReservationBLORemote
{

    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<Reservations> getAll()
    {
        String hql = "FROM Reservations";
        Query query = this.em.createQuery(hql);
        List<Reservations> reser = query.getResultList();
        return reser;
    }

    public Reservations getByID(String id)
    {
        Reservations reser = this.em.find(Reservations.class, id);
        return reser;
    }

    public String add(Accounts acc, List<ReservationDetails> resDetails)
    {
        try
        {
            float total = 0;
            for (ReservationDetails rDetail : resDetails)
            {
                float money = rDetail.getQuantity() * rDetail.getRestaurantTables().getDiningRooms().getRoomTypes().getSurcharge();
                total += money;
            }
            String resID = createResID();
            Reservations reservation = new Reservations(resID, new Date(), total, Boolean.FALSE);
            List<Reservations> reservations = new ArrayList<Reservations>();
            reservations.add(reservation);
            acc.setReservationsList(reservations);
            reservation.setAccounts(acc);

            this.em.merge(acc);

            //get last order
            acc = this.em.find(Accounts.class, acc.getId());
            reservations = acc.getReservationsList();
            Reservations lastReservation = reservations.get(reservations.size() - 1);
            //insert order details temp
            for (ReservationDetails resDetail : resDetails)
            {
                List<ReservationDetails> temp = new ArrayList<ReservationDetails>();
                temp.add(resDetail);
                lastReservation.setReservationDetailsList(temp);

                resDetail.setReservations(lastReservation);

                ReservationDetailsPK resDetailPK = new ReservationDetailsPK(lastReservation.getId(), resDetail.getRestaurantTables().getId());
                resDetail.setReservationDetailsPK(resDetailPK);
                this.em.merge(lastReservation);
            }
            return resID;
        }
        catch (Exception e)
        {
            return null;
        }
        
    }

    public String createResID()
    {
        String result = "";
        char[] chars =
        {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'
        };
        Random random = new Random();
        for (int i = 1; i <= 10; i++)
        {
            result += chars[random.nextInt(chars.length)];
        }
        return result;
    }

    public boolean edit(Reservations reservation)
    {
        return false;
    }

    public boolean delete(String id)
    {
        Reservations reser = getByID(id);
        if (reser != null)
        {
            try
            {
                em.remove(reser);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return false;
            }
            return true;
        }
        return false;
    }
}
